package com.liao.badminton.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.liao.badminton.code.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liao.badminton.dto.StipulationLevelAddsDto;
import com.liao.badminton.redis.RedisEnum;
import com.liao.badminton.redis.RedisUtil;
import com.liao.badminton.code.util.EntityUtils;
import com.liao.badminton.code.util.CheckUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.liao.badminton.entity.StipulationLevel;
import com.liao.badminton.dto.StipulationLevelDto;
import com.liao.badminton.vo.StipulationLevelListVo;
import com.liao.badminton.vo.StipulationLevelVo;
import com.liao.badminton.mapper.StipulationLevelMapper;
import com.liao.badminton.service.StipulationLevelService;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.stereotype.Component;

import lombok.extern.log4j.Log4j2;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import static cn.hutool.core.util.ObjectUtil.isEmpty;
import static cn.hutool.core.util.ObjectUtil.isNotEmpty;

/**
 * @author liaocheng
 * @date 2024-11-08
 * description:等级划分规则表
 */
@Component
@Log4j2
public class StipulationLevelServiceImpl extends ServiceImpl<StipulationLevelMapper, StipulationLevel> implements StipulationLevelService {


    /**
     * 新增
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, timeout = 10, rollbackFor = Exception.class)
    public ResponseRest saveStipulationLevel(List<StipulationLevelAddsDto> param) throws ProjectException {
        CheckUtil.checkValue(param, "param");
        List<StipulationLevel> boutStatDtoList = CollectionUtil.newArrayList();
        List<StipulationLevel> updateList = CollectionUtil.newArrayList();
        List<Long> deleteList = CollectionUtil.newArrayList();
        Long groupId = param.get(0).getGroupId();
        for (StipulationLevelAddsDto dto : param) {
            StipulationLevel stipulationLevel1 = dto.invertEntity();
            if (ObjectUtil.isNotEmpty(dto.getId())) {
                if (dto.getUseStatus() == 0) {
                    deleteList.add(dto.getId());
                }else {
                    EntityUtils.setUpdatedInfo(stipulationLevel1);
                    updateList.add(stipulationLevel1);
                }
            }else {
                EntityUtils.setCreatAndUpdateInfo(stipulationLevel1);
                boutStatDtoList.add(stipulationLevel1);
            }
        }
        RedisUtil.toDelete(RedisEnum.CACHE_TABLE_DATA, "stipulationLevel", "groupId",groupId);
        if (isNotEmpty(boutStatDtoList)) {
            baseMapper.addBatch(boutStatDtoList);
        }
        if (isNotEmpty(updateList)) {
            this.updateBatchById(updateList);
        }
        if (isNotEmpty(deleteList)) {
            baseMapper.deletes(deleteList);
        }
        return ResponseRest.suc("saveStipulationLevel", true);
    }

    /**
     * 删除
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, timeout = 10, rollbackFor = Exception.class)
    public ResponseRest deletesStipulationLevel(StipulationLevelDto param) throws ProjectException {
        this.listByIds(param.getDelIds()).stream().forEach(entity -> {
            RedisUtil.toDelete(RedisEnum.CACHE_TABLE_DATA, "stipulationLevel", "groupId",entity.getGroupId());
        });
        boolean bool = this.removeByIds(param.getDelIds());
        if (bool) {
            param.getDelIds().forEach(id -> RedisUtil.toDelete(RedisEnum.CACHE_TABLE_DATA, "stipulationLevel", "id", id));
        }
        return ResponseRest.suc("deletesStipulationLevel", true);
    }

    /**
     * 修改
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, timeout = 10, rollbackFor = Exception.class)
    public ResponseRest updateStipulationLevel(StipulationLevelDto param) throws ProjectException {
        StipulationLevel stipulationLevel = param.invertEntity();
        EntityUtils.setUpdatedInfo(stipulationLevel);
        boolean bool = this.updateById(stipulationLevel);
        if (bool) {
            RedisUtil.toDelete(RedisEnum.CACHE_TABLE_DATA, "stipulationLevel", "id", param.getId());
            RedisUtil.toDelete(RedisEnum.CACHE_TABLE_DATA, "stipulationLevel", "groupId",param.getGroupId());
        }
        return ResponseRest.suc("updateStipulationLevel", true);
    }

    /**
     * 查询列表
     */
    @Override
    public ResponseRest findStipulationLevelByPageParam(PageParam<StipulationLevelDto> pageParam) throws ProjectException {
        CheckUtil.checkValue(pageParam, "pageParam");
        CheckUtil.checkValue(pageParam.getArgs(), "pageParam.args");
        CheckUtil.checkValue(pageParam.getArgs().getGroupId(), "查询群ID不能为空");
        ResponseRest resp = ResponseRest.suc("findStipulationLevelByPageParam");
        PageHelper.startPage(pageParam.getCurrent(), pageParam.getSize(), "create_time desc");
        List<StipulationLevel> list = baseMapper.findByPageParam(pageParam);
        if (isEmpty(list)) { //没查到找模板数据给他
            list = this.list(new LambdaQueryWrapper<StipulationLevel>().eq(StipulationLevel::getGroupId, -1));
            list.stream().forEach(entity -> {
                entity.setGroupId(null);
                entity.setId(null);
            });
        }
        List<StipulationLevelVo> collect = list.stream().parallel().map(entity -> StipulationLevelVo.invertToVo(entity)).collect(Collectors.toList());
        resp.setData(new PageInfo<>(collect));
        return resp;
    }
    /**
     * 无分页查询列表
     */
    @Override
    public ResponseRest findListByParam(StipulationLevelDto param) throws ProjectException {
        CheckUtil.checkValue(param, "pageParam");
        CheckUtil.checkValue(param.getGroupId(), "查询群ID不能为空");
        ResponseRest resp = ResponseRest.suc("findStipulationLevelByPageParam");
        List<StipulationLevel> list = baseMapper.findByParam(param);
        List<StipulationLevelVo> collect = list.stream().parallel().map(entity -> StipulationLevelVo.invertToVo(entity)).collect(Collectors.toList());
       //没查到找模板数据给他
        List<StipulationLevel> templateList = this.list(new LambdaQueryWrapper<StipulationLevel>().eq(StipulationLevel::getGroupId, -1));
        List<StipulationLevelVo> templateVoList = templateList.stream().map(entity -> StipulationLevelVo.invertToVo(entity)).collect(Collectors.toList());
        StipulationLevelListVo stipulationLevelListVo = new StipulationLevelListVo();
        stipulationLevelListVo.setDefaultList(templateVoList);
        stipulationLevelListVo.setOwnerList(collect);
        return ResponseRest.suc("findStipulationLevelByPageParam", stipulationLevelListVo);
    }

    /**
     * 查询详情
     */
    @Override
    public ResponseRest findDetail(StipulationLevelDto param) throws ProjectException {
        Object getDate = RedisUtil.toGetDate(RedisEnum.CACHE_TABLE_DATA, "stipulationLevel", "id", param.getId());
        if (isNotEmpty(getDate)) {
            RedisUtil.toFlushExpire(RedisEnum.CACHE_TABLE_DATA, "stipulationLevel", "id", param.getId());
            return ResponseRest.suc("findDetail", (StipulationLevelVo) getDate);
        }
        StipulationLevelVo vo = null;
        StipulationLevel db = this.getById(param.getId());
        if (isNotEmpty(db)) {
            vo = StipulationLevelVo.invertToVo(db);
            RedisUtil.toSaveRedis(RedisEnum.CACHE_TABLE_DATA, "stipulationLevel", "id", param.getId(), vo, null);
        }
        return ResponseRest.suc("findDetail", vo);
    }

}
